/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package JSON;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;

/**
 *
 * @author guilherme
 */
@WebServlet(name = "RequestJSON_Clientes", urlPatterns = {"/RequestJSON_Clientes"})
public class RequestJSON_Clientes extends HttpServlet {

    public String cod_cliente = "1";

    public String getCod_cliente() {
        return cod_cliente;
    }

    public void setCod_cliente(String cod_cliente) {
        this.cod_cliente = cod_cliente;
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            try {
                Class.forName("com.mysql.jdbc.Driver");

                String connectionUrl = "jdbc:mysql://localhost/trabalho_ti_2np?" + "user=root&password=nicken2013";

                Connection con = (Connection) DriverManager.getConnection(connectionUrl);
                //@SuppressWarnings("unused")
                Statement stmt = (Statement) con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * from cliente where cli_id = " + this.getCod_cliente());

                JSONObject obj = new JSONObject();

                while (rs.next()) {
                    obj.put("cli_nome", new String(rs.getString("cli_nome")));
                    obj.put("cli_cpf", new String(rs.getString("cli_cpf")));
                    obj.put("cli_datanasc", new String(rs.getString("cli_datanasc")));
                    obj.put("cli_sexo", new String(rs.getString("cli_sexo")));
                    obj.put("cli_telresid", new String(rs.getString("cli_telresid")));
                    obj.put("cli_cel", new String(rs.getString("cli_cel")));
                    obj.put("cli_email", new String(rs.getString("cli_email")));
                    obj.put("cli_senha", new String(rs.getString("cli_senha")));
                    obj.put("cli_end", new String(rs.getString("cli_end")));
                    obj.put("cli_endnum", new String(rs.getString("cli_endnum")));
                    obj.put("cli_comp", new String(rs.getString("cli_comp")));
                    obj.put("cli_bairro", new String(rs.getString("cli_bairro")));
                    obj.put("cli_pontref", new String(rs.getString("cli_pontref")));
                    obj.put("cli_status", new String(rs.getString("cli_status")));
               }

                out.println(obj);
            } catch (SQLException e) {
                JSONObject obj = new JSONObject();
                obj.put("SQLException", new String(e.toString()));
                out.println(obj);
            } catch (ClassNotFoundException cE) {
                out.println("Class Not Found Exception: " + cE.toString());
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        this.cod_cliente = request.getParameter("cli_id");

    }

    //  String grid_code = request.getParameter("grid");
    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
